<?php 
class respond
{
	var $db;
	var $pages;
	var $number;
	var $table;
	var $modelid;

	function __construct()
	{
		global $db;
		$this->db = &$db;
		$this->table = DB_PRE.'respond';
		$this->table_review = DB_PRE.'review';
	}

	function respond()
	{
		$this->__construct();
	}

	function get($id)
	{
		$id = intval($id);
		if($id < 1) return false;
		return $this->db->get_one("SELECT * FROM $this->table WHERE id=$id");
	}


	function reviewsadd($id,$num='1')
	{
		$id = intval($id);
		$this->db->query("UPDATE `$this->table_review` SET respond=respond+$num WHERE `id`='$id' ");
		return true;
	}
    
	function add($data)
	{
		if(!is_array($data)) return false;
              $data[inputtime] = TIME;
		$this->db->insert($this->table, $data);
              $this->reviewsadd($data[reviewid]);
		return true;
	}


	function edit($data,$id)
	{
		if(!is_array($data)) return false;
		$this->db->update($this->table, $data,"id=$id");
		return true;
	}

	function delete($id)
	{
		if(is_array($id))
		{
			array_map(array(&$this, 'delete'), $id);
		}
		else
		{
			$id = intval($id);
                     $data = $this->get($id);
                    $this->reviewsadd($data[reviewid],'-1');
			$this->db->query("DELETE FROM `$this->table` WHERE `id`='$id' ");
		}
		return true;
	}


	
	function status($id, $status = 0)
	{
		$status = intval($status);
		if(is_array($id))
		{
			$id = implodeids($id);
			$this->db->query("UPDATE `$this->table` SET `status`='$status' WHERE `id` IN ($id)");
		}
		else
		{
			$this->db->query("UPDATE `$this->table` SET `status`='$status' WHERE `id`='$id' ");
		}
		return true;
	}

	function listinfo($where = '', $order = 'id DESC', $page = 1, $pagesize = 20)
	{
		if($where) $where = " WHERE $where";
		if($order) $order = " ORDER BY $order";
		$page = max(intval($page), 1);
		$offset = $pagesize*($page-1);
		$limit = " LIMIT $offset, $pagesize";

		$r = $this->db->get_one("SELECT count(*) AS number FROM $this->table $where");
		$number = $r['number'];
		$this->pages = pages($number, $page, $pagesize);
		$array = array();
		$result = $this->db->query("SELECT * FROM $this->table $where $order $limit");
		while($r = $this->db->fetch_array($result))
		{
			$array[] = $r;
		}
		$this->number = $this->db->num_rows($result);
		$this->db->free_result($result);
		return $array;
	}

}
?>